package net.sk.china.front.web;

import cn.hutool.core.util.ObjectUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.sk.china.common.request.PageParamRequest;
import net.sk.china.common.response.CommonPage;
import net.sk.china.common.response.CommonResult;
import net.sk.china.front.request.order.CartNumRequest;
import net.sk.china.front.request.order.CartRequest;
import net.sk.china.front.request.order.CartResetRequest;
import net.sk.china.front.response.order.CartInfoResponse;
import net.sk.china.front.service.order.StoreCartService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * +----------------------------------------------------------------------
 * | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2018-2024 聊来All rights reserved.
 * +----------------------------------------------------------------------
 * | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">apache</a> )
 * +----------------------------------------------------------------------
 * | @Author: 聊来 <18970881148@qq.com>
 * +----------------------------------------------------------------------
 * | DateTime: 2024/6/12 17:50
 * +----------------------------------------------------------------------
 */

@RestController
@RequestMapping("cart/")
@Api(tags = "购物车相关")
public class CartController {

    private final StoreCartService storeCartService;

    public CartController(StoreCartService storeCartService) {
        this.storeCartService = storeCartService;
    }

    @ApiOperation(value = "列表")
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public CommonResult<CommonPage<CartInfoResponse>> getList(@RequestParam(value = "isValid") Boolean isValid, @Validated PageParamRequest pageParamRequest) {
        CommonPage<CartInfoResponse> restPage = CommonPage.restPage(storeCartService.getList(pageParamRequest, isValid));
        return CommonResult.success(restPage);
    }

    @ApiOperation(value = "新增")
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public CommonResult<HashMap<String, Long>> save(@RequestBody @Validated CartRequest storeCartRequest) {
        Long cartId = storeCartService.saveCate(storeCartRequest);
        if (ObjectUtil.isNotNull(cartId)) {
            HashMap<String, Long> result = new HashMap<>();
            result.put("cartId", cartId);
            return CommonResult.success(result);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation(value = "删除")
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public CommonResult<String> delete(@RequestParam(value = "ids") List<Long> ids) {
        if (storeCartService.deleteCartByIds(ids)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation(value = "获取购物车数量")
    @RequestMapping(value = "count", method = RequestMethod.GET)
    public CommonResult<Map<String, Long>> count(@Validated CartNumRequest request) {
        return CommonResult.success(storeCartService.getCount(request));
    }

    @ApiOperation(value = "修改")
    @RequestMapping(value = "num", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "购物车ID", required = true, dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "number", value = "数量", required = true, dataTypeClass = Integer.class)
    })
    public CommonResult<String> update(@RequestParam(name = "id") Integer id, @RequestParam(name = "number") Integer number) {
        if (storeCartService.updateCartNum(id, number)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation(value = "购物车重选提交")
    @RequestMapping(value = "resetCart", method = RequestMethod.POST)
    public CommonResult<Object> resetCart(@RequestBody @Validated CartResetRequest resetRequest){
        return CommonResult.success(storeCartService.resetCart(resetRequest));
    }
}
